package com.springcloud.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.springcloud.config.feign.UserClient;
import com.springcloud.entity.UserEntity;

@RestController
public class MovieController {

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private DiscoveryClient discoveryClient;

	@Autowired
	private EurekaClient eurekaClient;

	@Autowired
	private LoadBalancerClient loadBalancerClient;

	@Autowired
	private UserClient userClient;

	@RequestMapping("/movie/get/{id}")
	public UserEntity mGet(@PathVariable long id) {
		List<String> services = discoveryClient.getServices();
		InstanceInfo instance = this.eurekaClient.getNextServerFromEureka("microservice-provider-user", false);
		for (String string : services) {
			System.err.println("services:" + string);
		}
		System.out.println("instance：" + instance);
		UserEntity user = restTemplate.getForObject("http://microservice-provider-user/get/" + id, UserEntity.class);
		return user;

	}

	@RequestMapping("/buy/{id}")
	public UserEntity buy(@PathVariable long id) {
		UserEntity userEntity1 = userClient.get(id);
		UserEntity userEntity2 = userClient.post(id);
		System.out.println("U1:" + userEntity1);
		System.out.println("U2:" + userEntity2);
		return userEntity1;

	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/get")
	@HystrixCommand(fallbackMethod = "defaultGet")
	public List<UserEntity> get() {
		List<UserEntity> list = restTemplate.getForObject("http://microservice-provider-user/list", List.class);
		return list;

	}

	@SuppressWarnings("unchecked")
	@RequestMapping("/post")
	// execution.isolation.strategy", value = "SEMAPHORE"
	// 当前线程操作，当设置位THREAD时，会另起一个线程来跑
	@HystrixCommand(fallbackMethod = "defaultPost", commandProperties = {
			@HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE") })
	public List<UserEntity> post() {

		List<UserEntity> list = restTemplate.getForObject("http://microservice-provider-user/list", List.class);
		return list;

	}

	public List<UserEntity> defaultGet() {
		System.err.println("defaultGet");
		UserEntity user = new UserEntity();
		user.setId(777L);
		List<UserEntity> list = new ArrayList<>();
		list.add(user);
		return list;

	}

	public List<UserEntity> defaultPost() {
		System.err.println("defaultPost");
		UserEntity user = new UserEntity();
		user.setId(888L);
		List<UserEntity> list = new ArrayList<>();
		list.add(user);
		return list;

	}

	@RequestMapping("/balance")
	public void testBalance() {
		ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider-user");
		System.out.println("111" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"
				+ serviceInstance.getPort());
		ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("microservice-provider-user2");
		System.out.println("222" + ":" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":"
				+ serviceInstance2.getPort());
	}
}
